﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using HCMServices.DTO;
using System.Data.Linq;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Collections.ObjectModel;

namespace HCMServices.DAO
{
    [Table(Name = "TUKHOADUONG")]
    public class KeyStreetDAO
    {
        [Column(Name = "MaTuKhoaDuong", IsDbGenerated = true, IsPrimaryKey = true, DbType = "int")]
        public int MaTuKhoaDuong { get; set; }

        [Column(Name = "TuKhoaDuong", DbType = "nvarchar(64)")]
        public string TuKhoaDuong { get; set; }

        [Column(Name = "MaDuong", DbType = "int")]
        public int MaDuong { get; set; }

        private MyDataContext db;

        public KeyStreetDAO(string connectString)
        {
            db = new MyDataContext(connectString);
        }

        private List<TUKHOADUONG> _allKeyStreetItems;

        public List<TUKHOADUONG> AllKeyStreetItems
        {
          get { return _allKeyStreetItems; }
          set { _allKeyStreetItems = value; }
        }

        public const string ConnectString = @"isostore:/Database.sdf";

        public List<KeyStreetDTO> LoadAllItems()
        {
            List<KeyStreetDTO> lstTuKhoaDuong = new List<KeyStreetDTO>();

            IList<TUKHOADUONG> query = (from TUKHOADUONG tkd in db.KeyStreet
                                        select tkd).ToList();

            foreach (TUKHOADUONG tkd in query)
            {
                if (tkd.MaDuong != null)
                {
                    KeyStreetDTO temp = new KeyStreetDTO();
                    temp.StreetId = (int)tkd.MaDuong;                       // NOTE !! tkd.MaDuong maybe null
                    temp.KeyStreetId = tkd.MaTuKhoaDuong;
                    temp.KeyStreet = tkd.TuKhoaDuong1;

                    lstTuKhoaDuong.Add(temp);
                }
            }

            return lstTuKhoaDuong;
        }

        public List<KeyStreetDTO> Search(string strInput)
        {
            List<KeyStreetDTO> lstTuKhoaDuongDTO = new List<KeyStreetDTO>();

            IList<TUKHOADUONG> query = (from TUKHOADUONG tkd in db.KeyStreet
                                        where tkd.TuKhoaDuong1 == strInput
                                        select tkd).ToList();

            foreach (TUKHOADUONG tkd in query)
            {
                if (tkd.MaDuong != null)
                {
                    KeyStreetDTO temp = new KeyStreetDTO();
                    temp.StreetId = (int)tkd.MaDuong;                             // note !! tkd maybe null
                    temp.KeyStreetId = tkd.MaTuKhoaDuong;
                    temp.KeyStreet = tkd.TuKhoaDuong1;

                    lstTuKhoaDuongDTO.Add(temp);
                }
            }

            return lstTuKhoaDuongDTO;
        }

        // Find something like strInput.
        public List<KeyStreetDTO> AutoCompleteTuKhoaDuong(string strInput)
        {
            List<KeyStreetDTO> lstTuKhoaDuong = new List<KeyStreetDTO>();

            IList<TUKHOADUONG> query = (from TUKHOADUONG tkd in db.KeyStreet
                                        where tkd.TuKhoaDuong1.IndexOf(strInput) > 0 
                                        select tkd).Distinct().Take(10).ToList();

            foreach (TUKHOADUONG tkd in query)
            {
                ExportResult(lstTuKhoaDuong, tkd);
            }

            return lstTuKhoaDuong;
        }

        private static void ExportResult(List<KeyStreetDTO> lstTuKhoaDuong, TUKHOADUONG tkd)
        {
            KeyStreetDTO temp = new KeyStreetDTO();
            temp.KeyStreet = tkd.TuKhoaDuong1;
            temp.KeyStreetId = tkd.MaTuKhoaDuong;
            if (null != tkd.MaDuong)
                temp.StreetId = (int)tkd.MaDuong;                   // Note !! tkd.MaDuong maybe null

            lstTuKhoaDuong.Add(temp);
        }

        // CAN THEO DOI YEU CAU KI HON
        public List<KeyStreetDTO> AutoCompleteTuKhoaDuongTheoDichVu(string strInput, int maDichVu)
        {
            List<KeyStreetDTO> lstTuKhoaDuong = new List<KeyStreetDTO>();

            var result = (from tkd in db.KeyStreet
                          from duong in db.Street
                          from dulieu in db.Data
                          where tkd.TuKhoaDuong1.IndexOf(strInput) > 0 && tkd.MaDuong == duong.MaDuong
                                 && duong.MaDuong == dulieu.MaDuong && dulieu.MaDichVu == maDichVu
                          select tkd).Distinct().Take(5).ToList();

            foreach (TUKHOADUONG tkd in result)
            {
                ExportResult(lstTuKhoaDuong, tkd);
            }

            return lstTuKhoaDuong;
        }

        public Dictionary<string, string> GetDictionaryOfKey()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            var query = (from tkd in db.KeyStreet
                         from duong in db.Street
                         where tkd.MaDuong == duong.MaDuong
                         select new { tkd.TuKhoaDuong1, duong.TenDuong }).ToList();

            foreach(var t in query)
            {
                if (t.TuKhoaDuong1 != null && !result.ContainsKey(t.TuKhoaDuong1))
                    result.Add(t.TuKhoaDuong1, t.TenDuong);
            }
            
            return result;
        }
    }
}
